﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Markup;
using Core = Vision.Core;

namespace Vision.Core.ActivityComponents.IO.File
{
    /// <summary>
    /// 
    /// </summary>            
    [ContentProperty("Body")]
    [System.ComponentModel.Designer(typeof(FileOpenDesigner))]
    public class FileOpen : NativeActivity
    {                        
        /// <summary>
        /// 
        /// </summary>        
        [Category("Input")]
        [DefaultValue(null)]
        public InArgument<string> FilePath { get; set; }
                        
        /// <summary>
        /// 
        /// </summary>
        [Category("Output")]
        [DefaultValue(null)]
        public OutArgument<byte[]> FileData { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Browsable(false)]
        [DefaultValue(null)]        
        public ActivityAction<byte[]> Body { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public FileOpen() : base()
        {
            this.Body = new ActivityAction<byte[]>(){
                Argument = new DelegateInArgument<byte[]>() { Name = "fileData" }
            };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="metadata"></param>
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            RuntimeArgument filePathArgument = new RuntimeArgument("FilePath", typeof(string), ArgumentDirection.In, true);
            metadata.Bind(this.FilePath, filePathArgument);
            metadata.AddArgument(filePathArgument);

            RuntimeArgument fullFileArgument = new RuntimeArgument("FileData", typeof(byte[]), ArgumentDirection.Out, false);
            metadata.Bind(this.FileData, fullFileArgument);
            metadata.AddArgument(fullFileArgument);
            
            metadata.AddDelegate(this.Body);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            byte[] fileData = null;
            if (System.IO.File.Exists(context.GetValue(this.FilePath)))
            {
                using (FileStream fStream = System.IO.File.Open(context.GetValue(this.FilePath), FileMode.Open, FileAccess.Read, FileShare.Read)) //System.IO.File.OpenRead(context.GetValue(this.FilePath)))
                {
                    fileData = new byte[fStream.Length];
                    fStream.Read(fileData,0,(int)fStream.Length);
                }
            }
            context.SetValue(this.FileData, fileData);

            // schedule the next action
            if (this.Body != null && this.Body.Handler != null)
            {
                context.ScheduleAction(this.Body, fileData);
            }
        }
    }
}
